package esempi.graphics;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * This example shows access to a Graphics2D. 
 * 
 * @author Alessandro Martinelli
 */
public class Esempio03Graphics2D extends JPanel{

	public static void main(String[] args) {
		JFrame frame1=new JFrame();
		frame1.setSize(270,70);
		frame1.setTitle("java.awt.Graphics");
		frame1.getContentPane().add(new Esempio03Graphics2D());
		frame1.setVisible(true);
		frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	@Override
	protected void paintComponent(Graphics graphics) {
		super.paintComponent(graphics);
	
		/*Il Graphics usato nelle interfacce è sempre
		 * un Graphics2D e un cast può consentire l'accesso
		 * ad un numero di funzionalità aggiuntive*/
		Graphics2D graphics2=(Graphics2D)graphics;
		graphics2.setBackground(Color.WHITE);
		
		//Rettangolo
		graphics2.setColor(Color.RED);
		graphics2.fillRect(10, 10, 30, 20);
		graphics2.setColor(Color.BLACK);
		graphics2.drawRect(10, 10, 30, 20);
		
		//Ovale
		graphics2.setColor(Color.GREEN);
		graphics2.fillOval(50, 10, 30, 20);
		graphics2.setColor(Color.BLACK);
		graphics2.drawOval(50, 10, 30, 20);
		
		//Triangolo
		graphics2.setColor(Color.YELLOW);
		int xs[]={90,90,120};
		int ys[]={10,30,30};
		graphics2.fillPolygon(xs,ys,3);
		graphics2.setColor(Color.BLACK);
		graphics2.drawPolygon(xs,ys,3);
		
		//Rettangolo con gli angoli smussati
		graphics2.setColor(Color.PINK);
		graphics2.fillRoundRect(130, 10, 30, 20, 10, 10);
		graphics2.setColor(Color.BLACK);
		graphics2.drawRoundRect(130, 10, 30, 20, 10, 10);
	
		//Arco d'Ellisse
		graphics2.setColor(Color.CYAN);
		graphics2.fillArc(170, 10, 30, 20, 0, 270);
		graphics2.setColor(Color.BLACK);
		graphics2.drawArc(170, 10, 30, 20, 0, 270);

	}
}
